package com.shinbada.common.tree;

import org.apache.commons.lang3.StringUtils;

/**
 * @author initcap
 * @date Created in 2020/12/14 22:40.
 */
public class TreeLevelUtil {

    public static final String SEPARATOR = ",";
    public static final String ROOT = "0";

    private TreeLevelUtil() {
        throw new IllegalArgumentException("util class can not instance!");
    }

    /**
     * @param parentLevel 父级的层级
     * @param id          新添加的编号
     *                    0,532162455578701
     *                    0,1,2
     *                    0,1,3
     *                    0,4
     */
    public static String calculateLevel(String parentLevel, String id) {
        if (StringUtils.isBlank(parentLevel)) {
            return StringUtils.join(ROOT, SEPARATOR, id);
        } else {
            return StringUtils.join(parentLevel, SEPARATOR, id);
        }
    }

    public static String currentLevel(String level, String id) {
        if (StringUtils.isBlank(level)) {
            return ROOT;
        } else {
            return level.replace(SEPARATOR + id, "");
        }
    }

}
